//
//  WBNavigationViewController.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/16.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import UIKit

class WBNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //        隐藏默认的navgationBar
        navigationBar.isHidden = true
    }

    
    /// 重写push的方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
            //        如果不是栈底控制器才需要隐藏
        if childViewControllers.count > 0 {
            //        隐藏底部的tabbar
            viewController.hidesBottomBarWhenPushed = true
            
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController{
                var title = "返回"
                //只有一个子控制器的时候显示上一级的标题,否则就显示返回
                if childViewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                //取出自定义的item
                let navigationSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
                navigationSpacer.width = -5;
                let leftItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParentVC), isBack:true)
                vc.navItem.leftBarButtonItems = [navigationSpacer,leftItem]
            }
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToParentVC(){
        popViewController(animated: true)
    }
}
